﻿using MediatR;
using W.Warehouse.Api.Application.Command.BinLocationCommand;
using W.Warehouse.Repository.Impl;
using W.Warehouse.Repository.Interface;

namespace W.Warehouse.Api.Application.CommandHandlder.BinLocationCommandHandlder
{
    public class BinLocationDelectCommandHandlder : IRequestHandler<BinLocationDelectCommand, ApiResult<int>>
    {
        private readonly ILogger<BinLocationDelectCommandHandlder> _logger;
        private readonly IBinLocationRepository _binLocationRepository;
        public BinLocationDelectCommandHandlder(IBinLocationRepository binLocationRepository, ILogger<BinLocationDelectCommandHandlder> logger)
        {
            _binLocationRepository = binLocationRepository;
            _logger = logger;
        }
        public async Task<ApiResult<int>> Handle(BinLocationDelectCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                _logger.LogInformation("删除库位管理成功");
                var res = await _binLocationRepository.GetModelAsync(request.binLocationId);
                res.IsDelete = true;
                res.DeleteTime = DateTime.Now;
                //res.DeleteName = "张三";
                result.Code = await _binLocationRepository.UpdateAsync(res);
            }
            catch (Exception el)
            {
                result.Code = -1;
                result.Message = el.Message;
                _logger.LogError(el, el.Message);
                throw;
            }
            return result;
        }
    }
}
